<?php
class Elsesky{
	/**
		 * Show GD created security image...
		 *
		 * @param  string $content
		 */
	public function show_gd_img($content="")
	{
		global $ibforums, $DB;

		$content = '  '. preg_replace( "/(\w)/", "\\1 ", $content ) .' ';

		@header("Content-Type: image/jpeg");

		$tmp_x = 140;
		$tmp_y = 20;

		$image_x = 210;
		$image_y = 65;

		$circles = 3;

		if ( $ibforums->vars['gd_version'] == 1 )
		{
			$tmp = imagecreate($tmp_x, $tmp_y);
			$im  = imagecreate($image_x, $image_y);
		}
		else
		{
			$tmp = imagecreatetruecolor($tmp_x, $tmp_y);
			$im  = imagecreatetruecolor($image_x, $image_y);
		}

		$white  = ImageColorAllocate($tmp, 255, 255, 255);
		$black  = ImageColorAllocate($tmp, 0, 0, 0);
		$grey   = ImageColorAllocate($tmp, 210, 210, 210 );

		imagefill($tmp, 0, 0, $white);

		for ( $i = 1; $i <= $circles; $i++ )
		{
			$values = array(
			0  => rand(0, $tmp_x - 10),
			1  => rand(0, $tmp_y - 3),
			2  => rand(0, $tmp_x - 10),
			3  => rand(0, $tmp_y - 3),
			4  => rand(0, $tmp_x - 10),
			5  => rand(0, $tmp_y - 3),
			6  => rand(0, $tmp_x - 10),
			7  => rand(0, $tmp_y - 3),
			8  => rand(0, $tmp_x - 10),
			9  => rand(0, $tmp_y - 3),
			10 => rand(0, $tmp_x - 10),
			11 => rand(0, $tmp_y - 3),
			);

			$randomcolor = imagecolorallocate( $tmp, rand(100,255), rand(100,255),rand(100,255) );
			imagefilledpolygon($tmp, $values, 6, $randomcolor );
		}

		imagestring($tmp, 5, 0, 2, $content, $black);

		//-----------------------------------------
		// Distort by resizing
		//-----------------------------------------

		imagecopyresized($im, $tmp, 0, 0, 0, 0, $image_x, $image_y, $tmp_x, $tmp_y);

		imagedestroy($tmp);

		$white   = ImageColorAllocate($im, 255, 255, 255);
		$black   = ImageColorAllocate($im, 0, 0, 0);
		$grey    = ImageColorAllocate($im, 100, 100, 100 );

		$random_pixels = $image_x * $image_y / 10;

		for ($i = 0; $i < $random_pixels; $i++)
		{
			ImageSetPixel($im, rand(0, $image_x), rand(0, $image_y), $black);
		}

		$no_x_lines = ($image_x - 1) / 5;

		for ( $i = 0; $i <= $no_x_lines; $i++ )
		{
			// X lines

			ImageLine( $im, $i * $no_x_lines, 0, $i * $no_x_lines, $image_y, $grey );

			// Diag lines

			ImageLine( $im, $i * $no_x_lines, 0, ($i * $no_x_lines)+$no_x_lines, $image_y, $grey );
		}

		$no_y_lines = ($image_y - 1) / 5;

		for ( $i = 0; $i <= $no_y_lines; $i++ )
		{
			ImageLine( $im, 0, $i * $no_y_lines, $image_x, $i * $no_y_lines, $grey );
		}

		ImageJPEG($im);
		ImageDestroy($im);

	}

	function show_gd_img_small($content="")
	{
		global $ibforums, $DB;

		$content = preg_replace( "/(\w)/", "\\1 ", $content );

		header("Content-Type: image/jpeg  charset=utf-8");

		$image_x = 50;
		$image_y = 20;
		$im  = imagecreatetruecolor($image_x, $image_y);
		$white  = ImageColorAllocate($im, 255, 255, 255);
		imagefill($im, 0, 0, $white);

		for ($i=1; $i<=100; $i++) {
			imageString($im,1,mt_rand(1,$image_y),mt_rand(1,$image_x),"*",imageColorAllocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
		}
		for ($i=0;$i<strlen($content);$i++){
			imageString($im, mt_rand(3,5),$i*$image_y/4+mt_rand(2,4),mt_rand(1,5), $content[$i],imageColorAllocate($im,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
		}
		ImageJPEG($im);
		ImageDestroy($im);
	}

	function clearHTML($str) // clean the HTML tag
	{
		$search = array (
		"'<[\/\!]*?[^<>]*?>'si",
		);

		$replace = array ("",
		);

		return preg_replace ($search, $replace, $str);

	}

	function PageLinkR($st,$total,$cut,$list)
	{

		$pages = ceil($total/$cut); //the total pages


		$st_page = ceil($st/$cut);  //current page



		$tmp_page = $pages - $st_page; //
		$pre_page = $st - $cut; //previous page
		$next_page = $st + $cut; //next page
		$last_page = $total - ($total % $cut); // the last page
		if($total % $cut == 0){$last_page = $last_page - $cut;}


		$tempstnum=(($st+$cut)<$total)?$st+$cut:$total;
		$totalnumbers = $st." - ".$tempstnum." of ".$total;

		$total_number .="<span class=\"pagelinkstat\">".$totalnumbers." </span>&nbsp;";

		$pagelink .= $total_number;

		//$pagelink .="<span class=\"pagelinkstat\">$pages Pages</span>&nbsp;";

		if($st_page >= 3)
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"".str_replace("<:st:>","0",$list)."\" title=\"Fisrt Page\">&laquo;</a></span>&nbsp;";
		}


		if($st_page >= 1)
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"".str_replace("<:st:>",$pre_page,$list)."\" title=\"Previous Page\">&lt;</a></span>&nbsp;";
		}


		if( $pages > 5 )
		{

			if( $st_page < 3 )
			{
				for($i=0;$i<5;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"".str_replace("<:st:>",$y,$list)."\" title=\"$y\">$x</a></span>&nbsp;";
				}

			}
			else
			{
				if($st_page+3 > $pages)
				{
					$startpage = $pages-5;
					$endpage = $pages-1;
				}
				else
				{
					$startpage = $st_page-2;
					$endpage = $st_page+2;
				}
				for($i=$startpage;$i<=$endpage;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"".str_replace("<:st:>",$y,$list)."\" title=\"$y\">$x</a></span>&nbsp;";
				}

			}

		}
		else
		{
			if($st_pages < 3)
			{
				for($i=0 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"".str_replace("<:st:>",$y,$list)."\" title=\"$y\">$x</a></span>&nbsp;";
				}
			}
			else
			{
				for($i=$pages -5 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"".str_replace("<:st:>",$y,$list)."\" title=\"$y\">$x</a></span>&nbsp;";
				}
			}
		}

		if ( $st_page+3 < $pages )
		{
			$pagelink .="<span class=\"pagelink\"><a href=\"".str_replace("<:st:>",$next_page,$list)."\" title=\"Next Page\">&gt;</a></span>&nbsp;";
		}

		if ( $st_page+4 < $pages )
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"".str_replace("<:st:>",$last_page,$list)."\" title=\"Last Page\">&raquo;</a></span>&nbsp;";
		}

		return $pagelink;
	}

	function PageLink($st,$total,$cut,$list)
	{
		$pages = ceil($total/$cut); //the total pages
		$st_page = ceil($st/$cut);  //current page
		$tmp_page = $pages - $st_page; //
		$pre_page = $st - $cut; //previous page
		$next_page = $st + $cut; //next page
		$last_page = $total - ($total % $cut); // the last page
		if($total % $cut == 0){$last_page = $last_page - $cut;}

		$total_number .="<span class=\"pagelink\">Total: ".$total." </span>&nbsp;";

		$pagelink .= $total_number;

		$pagelink .="<span class=\"pagelink\">$pages Pages</span>&nbsp;";

		if($st_page >= 3)
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"$list&st=0\" title=\"Fisrt Page\">&laquo;</a></span>&nbsp;";
		}


		if($st_page >= 1)
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"$list&st=$pre_page\" title=\"Previous Page\">&lt;</a></span>&nbsp;";
		}


		if( $pages > 5 )
		{

			if( $st_page < 3 )
			{
				for($i=0;$i<5;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"$list&st=$y\" title=\"$y\">$x</a></span>&nbsp;";
				}

			}
			else
			{
				if($st_page+3 > $pages)
				{
					$startpage = $pages-5;
					$endpage = $pages-1;
				}
				else
				{
					$startpage = $st_page-2;
					$endpage = $st_page+2;
				}
				for($i=$startpage;$i<=$endpage;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"$list&st=$y\" title=\"$y\">$x</a></span>&nbsp;";
				}

			}

		}
		else
		{
			if($st_pages < 3)
			{
				for($i=0 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"$list&st=$y\" title=\"$y\">$x</a></span>&nbsp;";
				}
			}
			else
			{
				for($i=$pages -5 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$class = "pagecurrent";
					}
					else
					{
						$class = "pagelinklast";
					}

					$pagelink .="<span class=\"$class\"><a href=\"$list&st=$y\" title=\"$y\">$x</a></span>&nbsp;";
				}
			}
		}

		if ( $st_page+3 < $pages )
		{
			$pagelink .="<span class=\"pagelink\"><a href=\"$list&st=$next_page\" title=\"Next Page\">&gt;</a></span>&nbsp;";
		}

		if ( $st_page+4 < $pages )
		{
			$pagelink .="<span class=\"pagelinklast\"><a href=\"$list&st=$last_page\" title=\"Last Page\">&raquo;</a></span>&nbsp;";
		}

		return $pagelink;
	}

	public function PageLinkFront($st,$total,$cut,$list)
	{
		$pages = ceil($total/$cut); //the total pages
		$st_page = ceil($st/$cut);  //current page
		$tmp_page = $pages - $st_page; //
		$pre_page = $st - $cut; //previous page
		$next_page = $st + $cut; //next page
		$last_page = $total - ($total % $cut); // the last page
		if($total % $cut == 0){$last_page = $last_page - $cut;}

		if($st_page >= 3)
		{
			$pagelink .="<a href=\"$list&st=0\" title=\"Fisrt Page\">&laquo;</a>&nbsp;";
		}


		if($st_page >= 1)
		{
			$pagelink .="<a href=\"$list&st=$pre_page\" title=\"Previous Page\">&lt;</a>&nbsp;";
		}


		if( $pages > 5 )
		{

			if( $st_page < 3 )
			{
				for($i=0;$i<5;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$pagelink .="<b>$x</b>&nbsp;";
					}
					else
					{
						$pagelink .="<a href=\"$list&st=$y\" title=\"$y\">$x</a>&nbsp;";
					}
				}

			}
			else
			{
				if($st_page+3 > $pages)
				{
					$startpage = $pages-5;
					$endpage = $pages-1;
				}
				else
				{
					$startpage = $st_page-2;
					$endpage = $st_page+2;
				}
				for($i=$startpage;$i<=$endpage;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$pagelink .="<b>$x</b>&nbsp;";
					}
					else
					{
						$pagelink .="<a href=\"$list&st=$y\" title=\"$y\">$x</a>&nbsp;";
					}
				}

			}

		}
		else
		{
			if($st_pages < 3)
			{
				for($i=0 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$pagelink .="<b>$x</b>&nbsp;";
					}
					else
					{
						$pagelink .="<a href=\"$list&st=$y\" title=\"$y\">$x</a>&nbsp;";
					}

				}
			}
			else
			{
				for($i=$pages -5 ;$i<$pages;$i++)
				{
					$x = $i+1;
					$y = $i * $cut;

					if($y == $st)
					{
						$pagelink .="<b>$x</b>&nbsp;";
					}
					else
					{
						$pagelink .="<a href=\"$list&st=$y\" title=\"$y\">$x</a>&nbsp;";
					}
				}
			}
		}

		if ( $st_page+3 < $pages )
		{
			$pagelink .="<a href=\"$list&st=$next_page\" title=\"Next Page\">&gt;</a>&nbsp;";
		}

		if ( $st_page+4 < $pages )
		{
			$pagelink .="<a href=\"$list&st=$last_page\" title=\"Last Page\">&raquo;</a>&nbsp;";
		}

		return $pagelink;
	}

	public function GetMonthArray($month = ''){
		$montharray = array();
		if ($month == '' || intval($month) > intval(date('n'))) {
			date('n');
			$montharray[] = mktime(0,0,0,date('n'),1,date('Y'));
			$montharray[] = time();
		}
		else {
			$montharray[] = mktime(0,0,0,$month,1,date('Y'));
			//��ʾĳ�µ׵�����
			$montharray[] = mktime(23,59,59,$month,date('t',mktime(0,0,0,$month,2,date('Y'))),date('Y'));
		}
		return $montharray;
	}

	//��֤�Ƿ�Ϊָ�����ȵ���ĸ/�������
	function fun_text1($num1,$num2,$str)
	{
		Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false;
	}

	//��֤�Ƿ�Ϊָ����������
	function fun_text2($num1,$num2,$str)
	{
		return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))?true:false;
	}
	//��֤�Ƿ�Ϊָ�����Ⱥ���
	function fun_font($num1,$num2,$str)
	{
		// preg_match("/^[\xa0-\xff]{1,4}$/", $string);
		return (preg_match("/^([\x81-\xfe][\x40-\xfe]){".$num1.",".$num2."}$/",$str))?true:false;
	}
	//��֤���֤����
	function fun_status($str)
	{
		return (preg_match('/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/',$str))?true:false;
	}

	//��֤�ʼ���ַ
	function fun_email($str){
		return (preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/',$str))?true:false;
	}
	//��֤�绰����
	function fun_phone($str)
	{
		return (preg_match("/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/",$str))?true:false;
	}
	//��֤�ʱ�
	function fun_zip($str)
	{
		return (preg_match("/^[1-9]\d{5}$/",$str))?true:false;
	}
	//��֤url��ַ
	function fun_url($str)
	{
		return (preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",$str))?true:false;
	}
	// ������ ת�� �����ַ� ����ֵ��Ϊ�ַ� �� һά����
	function data_join(&$data)
	{
		if(get_magic_quotes_gpc() == false)
		{
			if (is_array($data))
			{
				foreach ($data as $k => $v)
				{
					$data[$k] = addslashes($v);
				}
			}
			else
			{
				$data = addslashes($data);
			}
		}
		Return $data;
	}

	// ��ݳ�� ��ԭ �����ַ� ����ֵ��Ϊ�ַ� �� һ/��ά����
	function data_revert(&$data)
	{
		if (is_array($data))
		{
			foreach ($data as $k1 => $v1)
			{
				if (is_array($v1))
				{
					foreach ($v1 as $k2 => $v2)
					{
						$data[$k1][$k2] = stripslashes($v2);
					}
				}
				else
				{
					$data[$k1] = stripslashes($v1);
				}
			}
		}
		else
		{
			$data = stripslashes($data);
		}
		Return $data;
	}

	// �����ʾ ��ԭ ��ݸ�ʽ ��Ҫ����������� ����ֵ��Ϊ�ַ� �� һ/��ά����
	// ִ�д˷���ǰӦ��data_revert()���?��������˻�ԭ
	function data_show(&$data)
	{
		if (is_array($data))
		{
			foreach ($data as $k1 => $v1)
			{
				if (is_array($v1))
				{
					foreach ($v1 as $k2 => $v2)
					{
						$data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2]));
						$data[$k1][$k2]=str_replace(" ","&nbsp;",$data[$k1][$k2]);
						$data[$k1][$k2]=str_replace("\n","<br>\n",$data[$k1][$k2]);
					}
				}
				else
				{
					$data[$k1]=nl2br(htmlspecialchars($data[$k1]));
					$data[$k1]=str_replace(" ","&nbsp;",$data[$k1]);
					$data[$k1]=str_replace("\n","<br>\n",$data[$k1]);
				}
			}
		}
		else
		{
			$data=nl2br(htmlspecialchars($data));
			$data=str_replace(" ","&nbsp;",$data);
			$data=str_replace("\n","<br>\n",$data);
		}
		Return $data;
	}

}
?>